package com.borya.util;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 描述:
 * getCityUtil
 *
 * @outhor cyh
 * @create 2019-09-23 11:12
 */
public class CityUtils {

    public static List<Map<String, String>> addressResolution(String address) {
        String regex = "(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县|.*?区)(?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
        Matcher m = Pattern.compile(regex).matcher(address);
        String province, city, county, town, village;
        List<Map<String, String>> table = new ArrayList<>();
        Map<String, String> row;
        while (m.find()) {
            row = new LinkedHashMap<>();
            province = m.group("province");
            row.put("province", province == null ? "" : province.trim());
            city = m.group("city");
            row.put("city", city == null ? "" : city.trim());
            county = m.group("county");
            row.put("county", county == null ? "" : county.trim());
            town = m.group("town");
            row.put("town", town == null ? "" : town.trim());
            village = m.group("village");
            row.put("village", village == null ? "" : village.trim());
            table.add(row);
        }
        return table;
    }

    public static void main(String[] args) {
        List<Map<String, String>> table = addressResolution("四川省成都市武侯区天府大道");
        if (!Objects.isNull(table) && table.size() > 0) {
            System.out.println(table);
            System.out.println(table.get(0).get("province")); // 省份
            System.out.println(table.get(0).get("city")); // 城市
            System.out.println(table.get(0).get("county")); // 区、县
            System.out.println(table.get(0).get("town")); // 镇
            System.out.println(table.get(0).get("village")); // 余下
        } else {
            System.out.println("空数据");
        }


    }

}
